package com.heima.article.controller.v1;


import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.LoadBehaviorInfoVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/article")
public class ArticleHomeController {


    @Autowired
    private ApArticleService apArticleService;

    /**
     * 首页
     *
     * @param dto
     * @return
     */
    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto dto) {
        List<ApArticle> list = apArticleService.load2(dto, null,true);
        return ResponseResult.okResult(list);
    }

    /**
     * 加载更多
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto dto) {
        if (dto.getMinBehotTime() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<ApArticle> list = apArticleService.load2(dto, ArticleConstants.LOADTYPE_LOAD_MORE,false);
        return ResponseResult.okResult(list);
    }

    /**
     * 加载更新
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto dto) {
        if (dto.getMaxBehotTime() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<ApArticle> list = apArticleService.load2(dto, ArticleConstants.LOADTYPE_LOAD_NEW,false);
        return ResponseResult.okResult(list);
    }

    @PostMapping("/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) {
        Long id = apArticleService.saveArticle(dto);
        return ResponseResult.okResult(id);
    }

    /**
     * 加载文章数据回显
     * @param dto
     * @return
     */
    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto dto){
        if(dto.getAuthorId() == null || dto.getArticleId() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        LoadBehaviorInfoVO vo = apArticleService.loadArticleBehavior(dto);
        return ResponseResult.okResult(vo);
    }
}
